package com.yaorange.response;
import com.yaorange.response.enums.ResultCode;
import lombok.Data;

/**
 * 统一响应结果类
 */
@Data
public class Result<T> {

    private int code;        // 状态码
    private String message;  // 响应消息
    private T data;          // 响应数据


    public Result(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public Result(){}

    /**
     * 使用 ResultCode 构造成功/失败响应
     */
    public static <T> Result<T> of(ResultCode resultCode) {
        return new Result<>(resultCode.getCode(), resultCode.getMessage(), null);
    }

    public static <T> Result<T> of(ResultCode resultCode, T data) {
        return new Result<>(resultCode.getCode(), resultCode.getMessage(), data);
    }

    /**
     * 快捷方法：返回成功结果
     */
    public static <T> Result<T> success(T data) {
        return of(ResultCode.SUCCESS, data);
    }

    public static <T> Result<T> success() {
        return of(ResultCode.SUCCESS, null);
    }

    /**
     * 快捷方法：返回失败结果
     */
    public static <T> Result<T> fail() {
        return of(ResultCode.FAIL);
    }

    public static <T> Result<T> fail(ResultCode resultCode) {
        return of(resultCode);
    }

    public static <T> Result<T> fail(String message) {
        Result result = new Result();
        result.setCode(200);
        result.setMessage(message);
        return result;
    }

    public static <T> Result<T> fail(int code, String message){
        Result result = new Result();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }
}